package com.tiger.utils;

import java.math.BigInteger;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate5.HibernateTemplate;

public class HProxy {

	private static HibernateTemplate hibernateTemplate = Context.getBean(HibernateTemplate.class);

	private static SessionFactory sessionFactory = Context.getBean(SessionFactory.class);

	public static <T> void add(T t) {
		Session session = sessionFactory.openSession();
		session.save(t);
		session.flush();
		session.close();
	}

	public static <T> void update(T t) {
		Session session = sessionFactory.openSession();
		session.update(t);
		session.flush();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public static <T> T get(String hql, Object... args) {
		List<T> list = (List<T>) hibernateTemplate.find(hql, args);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		} else {
			return list.iterator().next();
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> list(String hql, Object... args) {
		Session session = sessionFactory.openSession();
		Query queryObject = session.createQuery(hql);
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				queryObject.setParameter(i, args[i]);
			}
		}
		List<T> list = (List<T>) queryObject.list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> list(String hql, Integer start, Integer size, Object... args) {
		Session session = sessionFactory.openSession();
		Query queryObject = session.createQuery(hql).setFirstResult(start).setMaxResults(size);
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				queryObject.setParameter(i, args[i]);
			}
		}
		List<T> list = (List<T>) queryObject.list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> listBySql(String sql, Class<T> clazz, Object... args) {
		Session session = sessionFactory.openSession();
		Query queryObject = session.createSQLQuery(sql);
		if(clazz != String.class) {
			queryObject = queryObject.setResultTransformer(Transformers.aliasToBean(clazz));
		}
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				queryObject.setParameter(i, args[i]);
			}
		}
		List<T> list = (List<T>) queryObject.list();
		session.close();
		return list;
	}

	public static int count(String hql, Object... args) {
		Session session = sessionFactory.openSession();
		Query queryObject = session.createQuery(hql);
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				queryObject.setParameter(i, args[i]);
			}
		}
		int size = ((Number)queryObject.uniqueResult()).intValue();
		session.close();
		return size;
	}
	
	public static int countBySql(String sql, Object... args) {
		Session session = sessionFactory.openSession();
		Query queryObject = session.createSQLQuery(sql);
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				queryObject.setParameter(i, args[i]);
			}
		}
		BigInteger count = (BigInteger) queryObject.list().iterator().next();
		session.close();
		return count.intValue();
	}
	
	
	public static String toInParams(int size) {
		StringBuilder sb = new StringBuilder("(");
		for(int i = 0; i < size; i++) {
			if(i > 0) {
				sb.append(",");
			}
			sb.append("?");
		}
		sb.append(")");
		return sb.toString();
	}

}
